package com.kaibo.roomdemo.activity

import android.databinding.DataBindingUtil
import android.graphics.Color
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.Toast
import com.android.databinding.library.baseAdapters.BR
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.kaibo.roomdemo.R
import com.kaibo.roomdemo.adapter.BaseDataBindingAdapter
import com.kaibo.roomdemo.data.bean.ItemDemoBean
import com.kaibo.roomdemo.data.bean.UserBean
import com.kaibo.roomdemo.databinding.ActivityDataBindingDemoBinding
import com.kaibo.roomdemo.databinding.ItemDataBindingDemoBinding
import com.kaibo.roomdemo.net.api.TestApi
import com.kaibo.roomdemo.net.http.HttpClientUtil
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_data_binding_demo.*
import kotlinx.android.synthetic.main.item_data_binding_demo.view.*
import java.util.*

class DataBindingDemoActivity : AppCompatActivity() {

    private var list: MutableList<ItemDemoBean> = ArrayList()

    init {
        (1..100)
                .map {
                    if (it % 2 == 0) {
                        Color.RED
                    } else {
                        Color.BLUE
                    }
                }
                .forEach {
                    list.add(ItemDemoBean())
                }
    }

    fun click1(btn: View) {
        Toast.makeText(this, "click1", Toast.LENGTH_SHORT).show()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_data_binding_demo)
        val contentView = DataBindingUtil.setContentView<ActivityDataBindingDemoBinding>(this, R.layout.activity_data_binding_demo)
        contentView.user = UserBean("王开波", 18)

        recyclerView.apply {
            adapter = object : BaseDataBindingAdapter<ItemDemoBean, ItemDataBindingDemoBinding>(R.layout.item_data_binding_demo) {
                override fun getItemVariableId(): Int = BR.itemDemoBean
            }.apply {
                setNewData(list)
            }

            layoutManager = LinearLayoutManager(this@DataBindingDemoActivity).apply {
                orientation = LinearLayoutManager.VERTICAL
            }
        }

//        val value= object : BaseQuickAdapter<Any, BaseViewHolder>(R.layout.item_data_binding_demo) {
//            override fun convert(helper: BaseViewHolder?, item: Any?) {
//                helper?.itemView?.hello?.text = "123"
//            }
//        }
//
//        value.data

        val inflate = layoutInflater.inflate(R.layout.item_data_binding_demo, null)
        inflate.hello.text = "123"

        //直接修改值
        list[20].apply {
            name = "哈哈"
            age = 1
            nameTextColor = Color.WHITE
            ageTextColor = Color.BLACK
        }

        contentView.imgUrl = "https://img2.mukewang.com/szimg/59897b1600011ec805400300.jpg"
        contentView.click = this

        HttpClientUtil
                .retrofit
                .create(TestApi::class.java)
                .getBaiduStr()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    println(it.string())
                }
    }
}
